package com.irdstudio.efp.flow.service.impl;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.flow.service.dao.BizFlowNodeDao;
import com.irdstudio.efp.flow.service.dao.BizFlowTaskDao;
import com.irdstudio.efp.flow.service.dao.BizFlowTaskParamDao;
import com.irdstudio.efp.flow.service.domain.BizFlowNode;
import com.irdstudio.efp.flow.service.domain.BizFlowTask;
import com.irdstudio.efp.flow.service.facade.BizFlowNodeService;
import com.irdstudio.efp.flow.service.vo.BizFlowNodeVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.SequenceConstance;
/**
 * <p>ServiceImpl: 业务流转节点信息				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizFlowNodeService")
public class BizFlowNodeServiceImpl extends AbstractFrameworkService implements BizFlowNodeService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizFlowNodeServiceImpl.class);

	@Autowired
	private BizFlowNodeDao bizFlowNodeDao;
	
	@Autowired
	private BizFlowTaskDao bizFlowTaskDao;
	@Autowired
	private BizFlowTaskParamDao bizFlowTaskParamDao;
	@Autowired
	private PatternedLimitableSeqService sequenceService;
	public static final PatternDescriptor PATTERN = DefaultPatternDescriptor.of("N" + SEQ, 6);
	/**
	 * 新增操作
	 */
	public int insertBizFlowNode(BizFlowNodeVO inBizFlowNodeVo) {
		logger.debug("当前新增数据为:"+ inBizFlowNodeVo.toString());
		int num = 0;
		try {
			BizFlowNode bizFlowNode = new BizFlowNode();
			inBizFlowNodeVo.setBizNodeId
			(sequenceService.getPatternedSequence(SequenceConstance.BIZ_FLOW_NODE_SEQ_ID, PATTERN));
			beanCopy(inBizFlowNodeVo, bizFlowNode);
			num = bizFlowNodeDao.insertBizFlowNode(bizFlowNode);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizFlowNodeVO inBizFlowNodeVo) {
		logger.debug("当前删除数据条件为:"+ inBizFlowNodeVo);
		int num = 0;
		int ret = 0;
		try {
			BizFlowNode bizFlowNode = new BizFlowNode();
			beanCopy(inBizFlowNodeVo, bizFlowNode);
			ret = num = bizFlowNodeDao.deleteByPk(bizFlowNode);
			logger.debug("根据条件:"+ inBizFlowNodeVo +"删除的数据条数为"+ num);
			//删除节点下任务
			List<BizFlowTask> taskList = bizFlowTaskDao.queryByBizNodeId(inBizFlowNodeVo.getBizNodeId());
			BizFlowTask bizFlowTask = new BizFlowTask();
			bizFlowTask.setBizNodeId(inBizFlowNodeVo.getBizNodeId());
			num = bizFlowTaskDao.deleteByBizNodeId(bizFlowTask);
			logger.debug("根据节点编号:"+ inBizFlowNodeVo +"删除的数据条数为"+ num);
			//删除任务参数
			if(taskList != null && taskList.size()>0) {
				for(int j=0; j<taskList.size(); j++) {
					BizFlowTask bizFlowTask1 = taskList.get(j);
					num = bizFlowTaskParamDao.deleteByTaskId(bizFlowTask1.getTaskId());
					logger.debug("任务编号:"+ bizFlowTask1.getTaskId() +"删除的数据条数为"+ num);
				}
			}
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			ret = -1;
		}
		return ret;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizFlowNodeVO inBizFlowNodeVo) {
		logger.debug("当前修改数据为:"+ inBizFlowNodeVo.toString());
		int num = 0;
		try {
			BizFlowNode bizFlowNode = new BizFlowNode();
			beanCopy(inBizFlowNodeVo, bizFlowNode);
			num = bizFlowNodeDao.updateByPk(bizFlowNode);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowNodeVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizFlowNodeVO queryByPk(BizFlowNodeVO inBizFlowNodeVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizFlowNodeVo);
		try {
			BizFlowNode queryBizFlowNode = new BizFlowNode();
			beanCopy(inBizFlowNodeVo, queryBizFlowNode);
			BizFlowNode queryRslBizFlowNode = bizFlowNodeDao.queryByPk(queryBizFlowNode);
			if (Objects.nonNull(queryRslBizFlowNode)) {
				BizFlowNodeVO outBizFlowNodeVo = beanCopy(queryRslBizFlowNode, new BizFlowNodeVO());
				logger.debug("当前查询结果为:"+ outBizFlowNodeVo.toString());
				return outBizFlowNodeVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowNodeVO> queryAllOwner(BizFlowNodeVO bizFlowNodeVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizFlowNodeVO> list = null;
		try {
			List<BizFlowNode> bizFlowNodes = bizFlowNodeDao.queryAllOwnerByPage(bizFlowNodeVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizFlowNodes.size());
			pageSet(bizFlowNodes, bizFlowNodeVo);
			list = (List<BizFlowNodeVO>) beansCopy(bizFlowNodes, BizFlowNodeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowNodeVO> queryAllCurrOrg(BizFlowNodeVO bizFlowNodeVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizFlowNode> bizFlowNodes = bizFlowNodeDao.queryAllCurrOrgByPage(bizFlowNodeVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizFlowNodes.size());
		List<BizFlowNodeVO> list = null;
		try {
			pageSet(bizFlowNodes, bizFlowNodeVo);
			list = (List<BizFlowNodeVO>) beansCopy(bizFlowNodes, BizFlowNodeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowNodeVO> queryAllCurrDownOrg(BizFlowNodeVO bizFlowNodeVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizFlowNode> bizFlowNodes = bizFlowNodeDao.queryAllCurrDownOrgByPage(bizFlowNodeVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizFlowNodes.size());
		List<BizFlowNodeVO> list = null;
		try {
			pageSet(bizFlowNodes, bizFlowNodeVo);
			list = (List<BizFlowNodeVO>) beansCopy(bizFlowNodes, BizFlowNodeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	/**
	 * 根据业务流转配置ID查询节点信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<BizFlowNodeVO> queryAllByFlowIdByPage(BizFlowNodeVO bizFlowNodeVo) {
		List<BizFlowNodeVO> list = null;
		try {
			List<BizFlowNode> bizFlowNodes = bizFlowNodeDao.queryAllByFlowIdByPage(bizFlowNodeVo);
			pageSet(bizFlowNodes, bizFlowNodeVo);
			list = (List<BizFlowNodeVO>) beansCopy(bizFlowNodes, BizFlowNodeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}
	
	/**
	 * 根据业务流转配置ID查询节点信息(不分页)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<BizFlowNodeVO> queryAllByFlowId(BizFlowNodeVO bizFlowNodeVo) {
		List<BizFlowNodeVO> list = null;
		try {
			List<BizFlowNode> bizFlowNodes = bizFlowNodeDao.queryAllByFlowId(bizFlowNodeVo);
			list = (List<BizFlowNodeVO>) beansCopy(bizFlowNodes, BizFlowNodeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	/**
	 * 根据业务流转配置ID和事件ID查询节点信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<BizFlowNodeVO> queryAllByFlowIdAndEventIdByPage(BizFlowNodeVO bizFlowNodeVo) {
		List<BizFlowNodeVO> list = null;
		try {
			List<BizFlowNode> bizFlowNodes = bizFlowNodeDao.queryAllByFlowIdAndEventIdByPage(bizFlowNodeVo);
			pageSet(bizFlowNodes, bizFlowNodeVo);
			list = (List<BizFlowNodeVO>) beansCopy(bizFlowNodes, BizFlowNodeVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}
}
